package logic

import (
	"context"
	"errors"
	"person/models/mysql"
	"person/personrpc/internal/svc"
	"person/personrpc/personrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type PerfectLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewPerfectLogic(ctx context.Context, svcCtx *svc.ServiceContext) *PerfectLogic {
	return &PerfectLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 完善信息
func (l *PerfectLogic) Perfect(in *personrpc.PerfectRequest) (*personrpc.PerfectResponse, error) {
	// todo: add your logic here and delete this line
	var user mysql.User

	err := user.FindUserByPhone(mysql.DB, in.Phone)
	if err != nil {
		return nil, errors.New("用户不存在")
	}

	user.Age = in.Age
	user.Address = in.Address

	err = user.UserUpdate(mysql.DB, in.UserId)
	if err != nil {
		return nil, errors.New("完善失败")
	}

	return &personrpc.PerfectResponse{
		UserId: int64(user.ID),
	}, nil
}
